<?php
namespace Amis\form;

use Amis\type\Api;
use Amis\Basics\AmisBasics;

class Form extends AmisBasics
{
    public string $type = 'form';

    public function name($name)
    {
        $this->name = $name;
        return $this;
    }

    public function mode($type = 'normal')
    {
        if (in_array($type, ['normal', 'horizontal', 'inline'])) {
            $this->mode = $type;
        }
        return $this;
    }

    /**
     * 当 mode 为 horizontal 时有用，用来控制 label
     * 默认值：{"left":"col-sm-2", "right":"col-sm-10", "offset":"col-sm-offset-2"}
     * @param object $horizontal
     * @return object
     */
    public function horizontal(object $horizontal)
    {
        return $this->horizontal = $horizontal;
    }

    public function title(string $title = '表单')
    {
        $this->title = $title;
        return $this;
    }

    public function submitText(string $submitText = "提交")
    {
        $this->submitText = $submitText;
        return $this;
    }

    public function className($className)
    {
        $this->className = $className;
        return $this;
    }

    public function body($body)
    {
        $this->body = $body;
        return $this;
    }

    public function actions(array $actions = [])
    {
        $this->actions = $actions;
        return $this;
    }

    public function messages($fetchSuccess = '', $fetchFailed = '', $saveSuccess = '', $saveFailed = '')
    {
        $this->messages['fetchSuccess'] = $fetchSuccess ?: '';
        $this->messages['fetchFailed']  = $fetchFailed ?: '';
        $this->messages['saveSuccess']  = $saveSuccess ?: '';
        $this->messages['saveFailed']   = $saveFailed ?: '';
        return $this;
    }

    public function wrapWithPanel(bool $wrapWithPanel = true)
    {
        $this->wrapWithPanel = $wrapWithPanel;
        return $this;
    }

    public function panelClassName(string $panelClassName = '')
    {
        $this->panelClassName = $panelClassName;
        return $this;
    }

    /**
     * @param \Amis\type\Api $api
     * @return $this
     */
    public function api(Api $api)
    {
        $this->api = $api;
        return $this;
    }

    /**
     * @param \Amis\type\Api $initApi
     * @return $this
     */
    public function initApi(Api $initApi)
    {
        $this->initApi = $initApi;
        return $this;
    }

    public function rules(array $rules = [])
    {
        $this->rules = $rules;
        return $this;
    }

    public function interval(int $interval = 3000)
    {
        $this->interval = $interval;
        return $this;
    }

    public function silentPolling(bool $silentPolling = false)
    {
        $this->silentPolling = $silentPolling;
        return $this;
    }

    public function stopAutoRefreshWhen(string $stopAutoRefreshWhen = '')
    {
        $this->stopAutoRefreshWhen = $stopAutoRefreshWhen;
        return $this;
    }

    /**
     *    Form 用来获取初始数据的 api,与 initApi 不同的是，会一直轮询请求该接口，直到返回 finished 属性为 true 才 结束。
     * @param Api $initAsyncApi
     * @return $this
     */
    public function initAsyncApi(Api $initAsyncApi)
    {
        $this->initAsyncApi = $initAsyncApi;
        return $this;
    }

    public function initFetch(bool $initFetch = true)
    {
        $this->initFetch = $initFetch;
        return $this;
    }

    public function initFetchOn(string $initFetchOn)
    {
        $this->initFetchOn = $initFetchOn;
        return $this;
    }

    public function initFinishedField(string $initFinishedField = 'finished')
    {
        $this->initFinishedField = $initFinishedField;
        return $this;
    }

    /**
     * @param \Amis\type\Api $asyncApi
     * @return $this
     */
    public function asyncApi(Api $asyncApi)
    {
        $this->asyncApi = $asyncApi;
        return $this;
    }

    /**
     *    轮询请求的时间间隔，默认为 3 秒。设置 asyncApi 才有效
     * @param int $checkInterval
     * @return $this
     */
    public function checkInterval(int $checkInterval = 3000)
    {
        $this->checkInterval = $checkInterval;
        return $this;
    }

    public function finishedField(string $finishedField = 'finished')
    {
        $this->finishedField = $finishedField;
        return $this;
    }

    public function submitOnChange(bool $submitOnChange = false)
    {
        $this->submitOnChange = $submitOnChange;
        return $this;
    }

    public function submitOnInit(bool $submitOnInit = false)
    {
        $this->submitOnInit = $submitOnInit;
        return $this;
    }

    public function resetAfterSubmit(bool $resetAfterSubmit = false)
    {
        $this->resetAfterSubmit = $resetAfterSubmit;
        return $this;
    }

    public function primaryField(string $primaryField = 'id')
    {
        $this->primaryField = $primaryField;
        return $this;
    }

    public function target(string $target)
    {
        $this->target = $target;
        return $this;
    }

    public function redirect(string $redirect)
    {
        $this->redirect = $redirect;
        return $this;
    }

    public function reload(string $reload)
    {
        $this->reload = $reload;
        return $this;
    }

    public function autoFocus(bool $autoFocus = false)
    {
        $this->autoFocus = $autoFocus;
        return $this;
    }

    public function canAccessSuperData(bool $canAccessSuperData = true)
    {
        $this->canAccessSuperData = $canAccessSuperData;
        return $this;
    }

    public function persistData(string $persistData = '')
    {
        $this->persistData = $persistData;
        return $this;
    }

    public function clearPersistDataAfterSubmit(bool $clearPersistDataAfterSubmit = true)
    {
        $this->clearPersistDataAfterSubmit = $clearPersistDataAfterSubmit;
        return $this;
    }

    public function preventEnterSubmit(bool $preventEnterSubmit = false)
    {
        $this->preventEnterSubmit = $preventEnterSubmit;
        return $this;
    }

    public function trimValues(bool $trimValues = false)
    {
        $this->trimValues = $trimValues;
        return $this;
    }

    public function promptPageLeave(bool $promptPageLeave = false)
    {
        $this->promptPageLeave = $promptPageLeave;
        return $this;
    }

    public function columnCount(int $columnCount = 0)
    {
        $this->columnCount = $columnCount;
        return $this;
    }

    public function inheritData(bool $inheritData = true)
    {
        $this->inheritData = $inheritData;
        return $this;
    }
}